// Pad.cpp: implementation of the CPad class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "NDSPad.h"
#ifdef PHONEOS
#include "Core/Commons.h"
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/*
#ifdef _WINDOWS
CPad * CPad::m_pPad = NULL;
CPad * CPad::GetPad()
{
	return m_pPad;
}
#else
#endif
*/
CPad::CPad()
{
/*
#ifdef _WINDOWS
	m_pPad = this;
#endif
*/	m_nCurrentKeyData = 0;
    //g_nKeyStatus
	memset(m_nKeyStatus,0,sizeof(m_nKeyStatus));
	memset(m_bKeyPressed,0,sizeof(m_bKeyPressed));
	memset(m_bKeyReleased,0,sizeof(m_bKeyReleased));
	memset(m_bKeyDoublePressed,0,sizeof(m_bKeyDoublePressed));
//	memset(&m_SpecialDribbleStatus,0,sizeof(m_SpecialDribbleStatus));
	//
//	ClearCache(TRUE,TRUE);
	m_nDirectionReleaseFrame[0] = m_nDirectionReleaseFrame[1] = m_nDirectionReleaseFrame[2] = m_nDirectionReleaseFrame[3] = 255;
//	DisableKeyPress(FALSE);
}

CPad::~CPad()
{

}

#define DOUBLEPRESSEDTIMESPLIT 10//700
void CPad::TranslateControlMessage(DWORD nKeyMap,bool bDown)
{
	//for key hold
	m_nKeyStatus[nKeyMap] = bDown;
	//for key pressed
	if(bDown)
	{
		if(!m_bKeyPressed[nKeyMap].bKeyNeedRelease)
		{
			m_bKeyPressed[nKeyMap].bKeyStatus = true;
			m_bKeyPressed[nKeyMap].bKeyHadBeenCheck = false;
			m_bKeyPressed[nKeyMap].bKeyNeedRelease = true;
		}
		//
	}
	//for key double pressed
	if(bDown)
	{
//		TRACE("Key %d pressed\n",nKeyMap);
		if(m_bKeyDoublePressed[nKeyMap].nKeyPressedState == 0)
		{
			m_bKeyDoublePressed[nKeyMap].nKeyPressedState = 1;
			m_bKeyDoublePressed[nKeyMap].nKeyPressedCounter = 0;

			m_bKeyDoublePressed[nKeyMap].bKeyStatus = FALSE;
			m_bKeyDoublePressed[nKeyMap].bKeyHadBeenCheck = FALSE;
		}
		else if(m_bKeyDoublePressed[nKeyMap].nKeyPressedState == 2)
		{
			if(m_bKeyDoublePressed[nKeyMap].nKeyPressedCounter < DOUBLEPRESSEDTIMESPLIT)
			{
				m_bKeyDoublePressed[nKeyMap].bKeyStatus = TRUE;
				m_bKeyDoublePressed[nKeyMap].bKeyHadBeenCheck = FALSE;

				m_bKeyDoublePressed[nKeyMap].nKeyPressedState = 3;
			}
			else
			{
				m_bKeyDoublePressed[nKeyMap].nKeyPressedState = 1;
				m_bKeyDoublePressed[nKeyMap].nKeyPressedCounter = 0;

				m_bKeyDoublePressed[nKeyMap].bKeyStatus = FALSE;
				m_bKeyDoublePressed[nKeyMap].bKeyHadBeenCheck = FALSE;
			}
		}
	}
	//for key released
	if(!bDown)
	{
		m_bKeyReleased[nKeyMap].bKeyStatus = true;
		m_bKeyReleased[nKeyMap].bKeyHadBeenCheck = false;
		//
//		m_bKeyDoublePressed[nKeyMap].bKeyHadBeenPressed = false;
//		TRACE("Key %d released\n",nKeyMap);
		m_bKeyPressed[nKeyMap].bKeyNeedRelease = false;
		//
		if(m_bKeyDoublePressed[nKeyMap].nKeyPressedState == 1)
		{
			m_bKeyDoublePressed[nKeyMap].nKeyPressedState = 2;
		}
	}
}

void CPad::UpdateKeyStatus()
{
	ReadKey();
	int i;
	for(i=0;i<KCD_KEYCOUNT;i++)
	{
		if(m_bKeyDoublePressed[i].bKeyStatus)
		{
			if(!m_bKeyDoublePressed[i].bKeyHadBeenCheck)
			{
				m_bKeyDoublePressed[i].bKeyHadBeenCheck = true;
			}
			else
			{
				m_bKeyDoublePressed[i].bKeyStatus = false;
				m_bKeyDoublePressed[i].bKeyHadBeenCheck = false;
				m_bKeyDoublePressed[i].nKeyPressedState = 0;
				m_bKeyDoublePressed[i].nKeyPressedCounter = 0;
//				TRACE("key %d double pressed had been reset\n",i);
			}
		}
		//
		if(m_bKeyDoublePressed[i].nKeyPressedState)
		{
			m_bKeyDoublePressed[i].nKeyPressedCounter ++;
		}
		//////////////////////////////////////////////////////////////////////////
		if(m_bKeyPressed[i].bKeyStatus)
		{
			if(!m_bKeyPressed[i].bKeyHadBeenCheck)
			{
				m_bKeyPressed[i].bKeyHadBeenCheck = true;
			}
			else
			{
				m_bKeyPressed[i].bKeyStatus = false;
				m_bKeyPressed[i].bKeyHadBeenCheck = false;
			}
		}
		if(m_bKeyReleased[i].bKeyStatus)
		{
			if(!m_bKeyReleased[i].bKeyHadBeenCheck)
			{
				m_bKeyReleased[i].bKeyHadBeenCheck = true;
			}
			else
			{
				m_bKeyReleased[i].bKeyStatus = false;
				m_bKeyReleased[i].bKeyHadBeenCheck = false;
			}
		}
	}
}

void CPad::UpdateKeyStatusRemote(unsigned short nKeyCode)
{
	ReadKeyRemote(nKeyCode);
	int i;
	for(i=0;i<KCD_KEYCOUNT;i++)
	{
		if(m_bKeyDoublePressed[i].bKeyStatus)
		{
			if(!m_bKeyDoublePressed[i].bKeyHadBeenCheck)
			{
				m_bKeyDoublePressed[i].bKeyHadBeenCheck = true;
			}
			else
			{
				m_bKeyDoublePressed[i].bKeyStatus = false;
				m_bKeyDoublePressed[i].bKeyHadBeenCheck = false;
				m_bKeyDoublePressed[i].nKeyPressedState = 0;
				m_bKeyDoublePressed[i].nKeyPressedCounter = 0;
//				TRACE("key %d double pressed had been reset\n",i);
			}
		}
		if(m_bKeyPressed[i].bKeyStatus)
		{
			if(!m_bKeyPressed[i].bKeyHadBeenCheck)
			{
				m_bKeyPressed[i].bKeyHadBeenCheck = true;
			}
			else
			{
				m_bKeyPressed[i].bKeyStatus = false;
				m_bKeyPressed[i].bKeyHadBeenCheck = false;
			}
		}
		if(m_bKeyReleased[i].bKeyStatus)
		{
			if(!m_bKeyReleased[i].bKeyHadBeenCheck)
			{
				m_bKeyReleased[i].bKeyHadBeenCheck = true;
			}
			else
			{
				m_bKeyReleased[i].bKeyStatus = false;
				m_bKeyReleased[i].bKeyHadBeenCheck = false;
			}
		}
	}
}

void CPad::KeyClick(int nKeyCode, bool bDown)
{
	TranslateControlMessage(nKeyCode,bDown);
	UpdateCache();
	OnKeyClick(nKeyCode,bDown);
}

void CPad::CheckKeyStatus(DWORD nMask,DWORD nKCDKey)
{
    unsigned short nStatus = (m_nCurrentKeyData & nMask);
    if(nStatus && !m_nKeyStatus[nKCDKey])
    {
     	KeyClick(nKCDKey,TRUE);
     	m_nKeyStatus[nKCDKey] = TRUE;
    }
    else if(!nStatus && m_nKeyStatus[nKCDKey]) 
    {
    	KeyClick(nKCDKey,FALSE);
    	m_nKeyStatus[nKCDKey] = FALSE;
    }
}

void CPad::ReadKey()
{
	UpdateCache();
}

void CPad::ReadKeyRemote(unsigned short nKeyCode)
{
	UpdateCache();
}

#define MAXSYNDIRFRAMECOUNT 3

void CPad::UpdateCache()
{
}

bool CPad::IsAnyKeyPressed()
{
	int i;
	for(i=0;i<KCD_KEYCOUNT;i++)
	{
		if(m_bKeyPressed[i].bKeyStatus)
			return true;
	}
	return false;
}

bool CPad::IsKeyPressed(int nKeyCode)
{	
//	if( KCD_START != nKeyCode  && m_nKeyStatus[KCD_START] )
//		return false;	
	return m_bKeyPressed[nKeyCode].bKeyStatus;
}

bool CPad::IsKeyHold(int nKeyCode)
{
	return m_nKeyStatus[nKeyCode];
}

bool CPad::IsKeyReleased(int nKeyCode)
{
	return m_bKeyReleased[nKeyCode].bKeyStatus;
}

bool CPad::IsKeyDoublePressed(int nKeyCode)
{
//	TRACE("check key %d double pressed status:%d\n",nKeyCode,m_bKeyDoublePressed[nKeyCode].bKeyStatus);
	return m_bKeyDoublePressed[nKeyCode].bKeyStatus;
}

void CPad::ClearKeyPress()
{
	int i;
/*
#ifdef _WINDOWS
	for(i=0;i<KCD_KEYCOUNT*2;i++)
#else*/
	for(i=0;i<KCD_KEYCOUNT;i++)
//#endif
		m_nKeyStatus[i] = false;
	memset(m_bKeyPressed,0,sizeof(m_bKeyPressed));
	memset(m_bKeyReleased,0,sizeof(m_bKeyReleased));
	memset(m_bKeyDoublePressed,0,sizeof(m_bKeyDoublePressed));
}

BOOL CPad::IsResetKeyHold()
{
	if(
	m_nKeyStatus[KCD_L] &&
	m_nKeyStatus[KCD_R] &&
	m_nKeyStatus[KCD_SELECT] &&
	m_nKeyStatus[KCD_START]
	)
		return TRUE;
	else
		return FALSE;
}
